var expelocal = window.expelocal || {};
//window.comHost = 'http://www.ikutrip.com';
expelocal.commonFun = function(){
	return {
		init:function(){
			var _this = this;
			_this.locationNav();
			_this.loadLocalFun();
			_this.sideBarFun();
			_this.loginPopup();
			_this.loginSubmitFun();
			_this.regSubmitFun();
			_this.searchFun();
		},
		searchFun:function(){
		    $('.submit-btn').on('click',function(){
		        var $this = $('.searchInput').val();
		        window.location.href = comHost + '/AllThemes-Default-1.html?keyword='+$this;
		        return false;
		    });
		    $(document).keyup(function(e){
		        var curKey = e.which; 
		        if(curKey==13){
		            $('.submit-btn').trigger('click');	
		        };
		    });
		},
		// 目的地弹出层
		locationNav:function(){
			var _this = this;
			var $header = $(".head-main"),
				$winW = $(window).width();

	        $header.find(".nav .items").on("mouseenter", "li", function() {
	            $this = $(this);
	            $this.addClass("cur").find(".dropbox").show()
	        });
	        $header.find(".nav .items").on("mouseleave", "li", function(e) {
		        var $dropbox, $self;
		        $self = $(this);
		        $self.removeClass("cur");
		        $dropbox = $self.find(".dropbox");
		        if ($dropbox.length) {
		            $dropbox.hide()
		        }
		    });
		},
		loadLocalFun:function(){
		    $.ajax({
		        url: "/js/LocationNavi.html", dataType: "json", success: function (data) {
				var _html = '',
					nData = data.data;
				_html += '<div class="na-items cf">';
				_html += '<div class="continent">';
				for(var i = 0 ; i <nData.location.length; i++){
					_html += '<p>'+ nData.location[i].continent_name +'</p>';
				};
				_html += '</div>';
				for(var i = 0 ; i <nData.location.length; i++){
					if(i == 0){
						_html += '<div class="country-main" style="display:block;">';
					}else{
						_html += '<div class="country-main">';
					}

					for(var j = 0 ; j <nData.location[i].countrylist.length; j++){
						var aLocaArr = nData.location[i].countrylist[j];
						_html += '<div class="country cf">';
						_html += '<dl class="itmes"><dt><a href="'+ aLocaArr.country_url +'">'+ aLocaArr.country_name +'</a></dt>';
						if(aLocaArr.citylist){
							for(var k = 0 ; k < aLocaArr.citylist.length; k++){
								var bLocaArr = aLocaArr.citylist[k];
								if(bLocaArr.ishot == "Y"){
									_html += '<dd><a href="'+ bLocaArr.city_url +'" class="hot">'+ bLocaArr.city_name +'</a></dd>';
								}else{
									_html += '<dd><a href="'+ bLocaArr.city_url +'">'+ bLocaArr.city_name +'</a></dd>';
								}
							}
						}
						
						_html += '</dl>';
						_html += '</div>';
					};
					_html += '</div>';
				};
				_html += '</div>';
	        	$("#destination_nav").prepend(_html);
	        }});
			$("#destination_nav").on('mouseenter','.continent p',function(){
				var $this = $(this),
					_index = $this.index();
				$("#destination_nav").find('.country-main').hide().eq(_index).show();
				$this.addClass('hover').siblings('p').removeClass('hover');
			});
		},
		sideBarFun:function(){
			var $sideCont = $("#side-bar"),
		        $window = $(window),
		        winH = $window.height();
		    $sideCont.find(".items li").hover(function() {
		        var $this = $(this);
		        $this.find(".list").show()
		    }, function() {
		        var $this = $(this);
		        $this.find(".list").hide()
		    });
		    $window.scroll(function() {
		        var scrTop = $window.scrollTop();
		        var _top = $("#side-bar").offset().top;
		        if (scrTop > winH) {
		            $sideCont.find(".goTop").show()
		        } else {
		            $sideCont.find(".goTop").hide()
		        }
		    });
		    $sideCont.find(".items").on("click", ".goTop a", function() {
		        $("html,body").animate({
		            scrollTop: 0
		        }, 500)
		    })
		},
		// 登录注册弹层
		loginPopup:function(){
			// layer.open({
			// 	  type: 1,
			// 	  title: false,
			// 	  area: '594px',
			// 	  skin: 'layui-layer-nobg',
			// 	  shadeClose: true,
			// 	  content: $('#mobileLoign')
			// 	});
			$('.login-main').on('click','.inc_login',function(){
				layer.open({
				  type: 1,
				  title: false,
				  area: '594px',
				  skin: 'layui-layer-nobg',
				  shadeClose: true,
				  content: $('#mobileLoign')
				});
			});
			$('.login-main').on('click','.inc_reg',function(){
				layer.open({
				  type: 1,
				  title: false,
				  area: '594px',
				  skin: 'layui-layer-nobg',
				  shadeClose: true,
				  content: $('#regPopup')
				});
			});
			$('#regBtn').on('click',function(){
				layer.closeAll();
				layer.open({
				  type: 1,
				  title: false,
				  area: '594px',
				  skin: 'layui-layer-nobg',
				  shadeClose: true,
				  content: $('#regPopup')
				});
			});
			$('#loginBtn').on('click',function(){
				layer.closeAll();
				layer.open({
				  type: 1,
				  title: false,
				  area: '594px',
				  skin: 'layui-layer-nobg',
				  shadeClose: true,
				  content: $('#mobileLoign')
				});
			});
		},
		//登录提交
		loginSubmitFun:function(){
			var flag = false,
				phoneCode = false;
				_this = this;
			$('#loginPhone').keyup(function(e) {
				var userVal = $('#loginPhone').val();
				if(_this.isMobile(userVal)){
					$('#phoneCode').show();
					phoneCode = true;
				}else if(_this.isEmail(userVal)){
					$('#phoneCode').hide();
					phoneCode = false;
				}else{
					$('#phoneCode').hide();
					phoneCode = false;
				};
			});
			$('#loginPopupBtn').on('click',function(){
			    var userVal = $('#loginPhone').val(),
					password = $('#loginPassword').val(),
					phoneNum = "",
					email = "";
				if(userVal == ''){
					layer.open({
						title: false,
						type: 0,
						content:'手机／邮箱不能为空！'
					});
					return;
				}else{
					if(_this.isMobile(userVal)){
						phoneNum = userVal;
					}else if(_this.isEmail(userVal)){
						email = userVal;
					}else{
						layer.open({
							title: false,
							type: 0,
							content:'请输入正确的手机号／邮箱！'
						});
						return;	
					}
				};
				if(password == ''){
					layer.open({
						title: false,
						type: 0,
						content:'密码不能为空！'
					});
					return;
				};
				$.ajax({
		            type: 'POST',
		            url:comHost + '/account/login',
		            data:{
		            	phone:phoneNum,
		            	email:email,
		            	pwd:password
		            },
		            dataType: "jsonp",
		            jsonp: 'jsonpCallback',
		            success: function (data) {
		       
		            	if(data.code == "ok"){
		            		window.location.href = window.location.href;
		            	}else{
		            		layer.open({
								title: false,
								type: 0,
								content:data.msg
							});
		            	}
		            	
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
		                //alert(XMLHttpRequest.status);
		                //alert(XMLHttpRequest.readyState);
		                //alert(textStatus);
		            }
		        })
			});
		},
		//注册提交
		regSubmitFun:function(){
			var flag = false,
				phoneCode = false;
				_this = this;

			$('#getCode').on('click',function(){
				var time = 60,
					$this = $(this);
				var userVal = $('#regPhone').val();
				var timeTask = setInterval(function(){
		            if(time <= 0){
		                $this.val('重新获取验证码').prop('disabled',false);
		                clearInterval(timeTask);
		                return;
		            }
		            $this.val((--time)+"秒后重新获取");
		        },1000);
		        $.ajax({
		            type: 'POST',
		            url:comHost + '/member/SendCheckMessage',
		            data:{
		            	phone:userVal
		            },
		            dataType: "jsonp",
		            jsonp: 'jsonpCallback',
		            success: function(data){
		            	if(data.code != "ok"){
		            		layer.open({
								title: false,
								type: 0,
								content:data.msg
							});
		            	};
		            }
		        });
			});
			$('.chageCodeBtn').on('click',function(){
			    $("#vcode").attr('src', '/account/VCode?' + Math.random());
			});
			$('#imgCode').keyup(function(e) {
				var userVal = $('#imgCode').val();
				if(userVal.length  == 4){
					$.ajax({
					    type: 'POST',
					    data: {
					        code: userVal
					    },
			            url:comHost + '/account/CheckVCode',
			            dataType: "jsonp",
			            jsonp: 'jsonpCallback', 
			            success: function(data){ 
			            	if(data.result!= "ok"){
			            		layer.open({
									title: false,
									type: 0,
									content:'验证码不正确！'
								});
			            	}
			            }
			        });
		        }
			});
			$('#regPhone').keyup(function(e) {
				var userVal = $('#regPhone').val();
				if(_this.isMobile(userVal)){
					$('#phoneCode').show();
					phoneCode = true;
				}else if(_this.isEmail(userVal)){
					$('#phoneCode').hide();
					phoneCode = false;
				}else{
					$('#phoneCode').hide();
					phoneCode = false;
				};
			});
			$('#regPopupBtn').on('click',function(){
				var userVal = $('#regPhone').val(),
					imgCode = $('#imgCode').val(),
					yzmCode = $('#yzmCode').val(),
					password = $('#regPassword').val(),
					phoneNum = "",
					email = "";
				if(userVal == ''){
					layer.open({
						title: false,
						type: 0,
						content:'手机／邮箱不能为空！'
					});
					return;
				}else{
					if(_this.isMobile(userVal)){
						phoneNum = userVal;
					}else if(_this.isEmail(userVal)){
						email = userVal;
					}else{
						layer.open({
							title: false,
							type: 0,
							content:'请输入正确的手机号／邮箱！'
						});
						return;	
					}
				};
				if(imgCode == ''){
					layer.open({
						title: false,
						type: 0,
						content:'验证码不能为空！'
					});
					return;
				};
				if(phoneCode){
					if(yzmCode == ''){
						layer.open({
							title: false,
							type: 0,
							content:'手机验证码不能为空！'
						});
						return;
					};
				};
				if(password == ''){
					layer.open({
						title: false,
						type: 0,
						content:'密码不能为空！'
					});
					return;
				};

				if(!$( "input:checked" ).length){
					layer.open({
						title: false,
						type: 0,
						content:'请查看服务条款！'
					});
					return;
				}
				$.ajax({
		            type: 'POST',
		            url:comHost + '/account/register',
		            data:{
		            	phone:phoneNum,
		            	email:email,
		            	pwd:password,
		            	messagecode:yzmCode
		            },
		            dataType: "jsonp",
		            jsonp: 'jsonpCallback',
		            success: function(data){
		            	if(data.code == "ok"){
		            		window.location.href = window.location.href;
		            	}else{
		            		layer.open({
								title: false,
								type: 0,
								content:data.msg
							});
		            	}
		            	
		            }
		        })
			});
		},
		isMobile:function(value){
			return /^(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/.test(value);
		},
		isEmail:function(value){
			return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]{2,})+$/.test(value);
		},
	}
};
$(function(){
	var comFun = new expelocal.commonFun();
	comFun.init();
});